import { Viewer, Ion } from 'cesium';
import { MyCesiumConsts } from '@/constant';
import ViewerInitConfig from './viewer-init-config';
import 'widgets.css';

class SceneViewer {

    viewerInitConfig;
    viewerInstance;

    constructor(viewerInitConfig, viewerInstance) {
        this.viewerInitConfig = viewerInitConfig;
        this.viewerInstance = viewerInstance;
    }

    // create Cesium.Viewer singleton
    static getInstance(viewerDomId, extendConfigObj) {
        const sceneViewer = window.viewer;
        // create Viewer
        if (!sceneViewer || sceneViewer == null) {
            var domId = '';

            // test viewer dom id
            const viewerDom = $(viewerDomId)[0];
            domId = viewerDom && viewerDom != null ? viewerDomId : MyCesiumConsts.DEFAULT_CONTAINER_DOM_ID;

            // set user private cesium token before creating global viewer instance
            // all default operators or hints should be hidden, so the step is not required
            Ion.defaultAccessToken = MyCesiumConsts.ACCESS_TOKEN;

            // default Cesium.Viewer config properties
            const viewerInitConfig = new ViewerInitConfig();
            var configResult;

            // combine extended config properties
            if (extendConfigObj && extendConfigObj != null && typeof extendConfigObj === 'object') {
                configResult = viewerInitConfig.extendDefaultConfig(extendConfigObj);
            } else {
                configResult = viewerInitConfig.getDefaultConfig();
            }

            // default Cesium.Viewer
            var viewerInstance =  new Viewer(domId, configResult);
            // hide icons and hints in bottom containers
            viewerInstance._cesiumWidget._creditContainer.style.display = 'none';

            // create global custom viewer object and mount it to present window
            window.viewer = new SceneViewer(viewerInitConfig, viewerInstance);
        }
        return window.viewer;
    }
}

export default SceneViewer;